#! /bin/bash
# ----------------------------------------------------------------------------
#
# This script will plot forever.
#
# yqm_leaf@163.com
#
# 05/03/21
#
# ----------------------------------------------------------------------------

usage () {
    echo "$0 -t [tmp dir] -d [destination dir] -f [farmer key] -p [pool key]"
}

gen_log_filename () {
    local num=50 # let's make it big.
    local prefix="plotting"
    local suffix=".log"
    for i in $(seq 1 ${num}) ; do
        local file_name="${prefix}${i}${suffix}"
        [ -a ${file_name} ] && continue
        break
    done
    # This will overwite 50th log file if files exists.
    echo ${file_name}
}

while getopts ":t:d:f:p:" opt ; do
  case $opt in
    t)
      TEMP_DIR=${OPTARG};;
    d)
      DST_DIR=${OPTARG};;
    f)
      FARMER_KEY=${OPTARG};;
    p)
      POOL_KEY=${OPTARG};;
    \?)
      usage
      exit 1;;
    :)
      echo "Option -$OPTARG requires an argument. -h to see more."
      exit 1;;
  esac
done

[ -z $FARMER_KEY ] || [ -z $POOL_KEY ] || [ -z $TEMP_DIR ] || [ -z $DST_DIR ] && \
    { usage ; exit 1 ; }

while : ; do
    LOG_FILE=$(gen_log_filename)
    chia plots create -n 1 -r 4 -b 4608 -f ${FARMER_KEY} -p ${POOL_KEY} -t ${TEMP_DIR} -d ${DST_DIR} | tee ${LOG_FILE}
done
